package com.ym.health.api.patient.self;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroTokenManager;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.UserPatientViewEntity;
import com.ym.medical.domain.vo.LoginVO;
import com.ym.medical.service.IUserSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *
 */
@Api(tags = "患者端-我的-相关接口")
@RestController
@Validated
@RequestMapping(value = "/patient/self", headers = Constants.Api.VERSION)
public class UserController extends BaseController {

	@Autowired
	private IUserSV userSV;

	@Resource
	private WxMaService wxMaService;

	@Log
	@ApiOperation("授权手机号")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "encryptedData", value = "加密后的手机号码", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "iv", value = "向量", required = true, paramType = "query", dataType = "String")
	})
	@ApiSign(isSign = false)
	@PostMapping(value = "/authorize/phone", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse authorizedPhone(String encryptedData, String iv) {
		WxMaUserService userService = wxMaService.getUserService();
		UserPatientViewEntity loginUser = (UserPatientViewEntity) ShiroUtil.getLoginUser();
		WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(loginUser.getSessionKey(), encryptedData, iv);
		userSV.authorizedPhone(loginUser.getId(), phoneNoInfo.getPhoneNumber());
		userSV.refreshPatientUser();
		return successMsg(phoneNoInfo.getPhoneNumber());
	}


	@Log
	@ApiOperation(value = "授权用户个人信息", notes = "授权前，请检查sessionKey是否过期，过期请重新执行登录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "encryptedData", value = "加密后的用户信息", required = true, paramType = "form"),
			@ApiImplicitParam(name = "iv", value = "向量", required = true, paramType = "form"),
			@ApiImplicitParam(name = "recommendUserId", value = "用户id", required = false, paramType = "form")
	})
	@ApiSign(isSign = false)
	@PostMapping(value = "/authorize/info", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<LoginVO> authorizeInfo(String encryptedData, String iv, Long recommendUserId) {
		UserPatientViewEntity loginUser = (UserPatientViewEntity) ShiroUtil.getLoginUser();
		LoginVO loginVO = userSV.authorizeInfo(loginUser, encryptedData, iv, recommendUserId);
		ShiroTokenManager.bindUserToken();
		return successData(loginVO);
	}

}
